package com.itdev.matcherapp.android.gtalkclient;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.provider.Im;
import android.widget.Toast;

import com.itdev.matcherapp.android.R;

public class GTalkService extends Service {

	private final IBinder mBinder = new LocalBinder();
	private NotificationManager mNM;
	private ContentResolver contentResolver;
	private Cursor cursorInvited;
	private Handler handler;
	private SoundEffectPlayer soundPlayer = new SoundEffectPlayer(this);

	public class LocalBinder extends Binder {
		GTalkService getService() {
			return (GTalkService) GTalkService.this;
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	@Override
	protected void onCreate() {
		handler = new Handler();
		MyObserver observer = new MyObserver(handler);
		contentResolver = getContentResolver();
		cursorInvited = contentResolver.query(Im.Subscriptions.CONTENT_URI,
				null, null, null, null);
		cursorInvited.registerContentObserver(observer);
		mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		showMessage("Create");
	}

	@Override
	protected void onDestroy() {
		showMessage("Destroy");
		mNM.cancel(R.string.app_name);
		Toast.makeText(this, "Stop Service", Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onStart(int startId, Bundle arguments) {
		showMessage("Start "+Integer.toString(startId));
//		Timer t = new Timer();
//		TimerTask tt = new TimerTask(){
//
//			@Override
//			public void run() {
//				Intent intent = new Intent(GTalkService.this, ContactList.class);
//				intent.addLaunchFlags(Intent.NEW_TASK_LAUNCH);
//				startActivity(intent);
//			}
//			
//		};
//		t.schedule(tt, 5000);
	}

	private void showMessage(String message) {
		Intent contentIntent = new Intent(this, GTalkService.class);
		Intent apIntent = new Intent(this, GTalkRequestList.class);
		Notification notification = new Notification(this, R.drawable.icon,
				"GTalk Service", System.currentTimeMillis(), message, "3",
				contentIntent, R.drawable.icon, "GTalk Friend Request", apIntent);
		mNM.notify(R.string.app_name, notification);
		Toast.makeText(this, message + "Please see in slider", Toast.LENGTH_LONG).show();
	}
	
	private class MyObserver extends ContentObserver{

		public MyObserver(Handler handler) {
			super(handler);
		}

		@Override
		public void onChange(boolean arg0) {
			String request = new String("-");
			//cursorInvited = contentResolver.query(Im.Subscriptions.CONTENT_URI, null, null, null, null);
			soundPlayer.playTong();
			cursorInvited.requery();
			int columnIndex = cursorInvited.getColumnIndex(Im.Subscriptions.USERNAME);
			int count = cursorInvited.count();
			if(count > 0)
				cursorInvited.next();
			if(cursorInvited.first()){
				do{
					request = request+", "+cursorInvited.getString(columnIndex);
				}while(cursorInvited.next());
			}
			String display = "friend request("+count+")";
			showMessage(display);
//			
//			Intent intent = new Intent(GTalkService.this, ContactList.class);
//			intent.addLaunchFlags(Intent.NEW_TASK_LAUNCH);
//			startActivity(intent);
			
		}
    	
    }
}
